package fy.mdm.dao;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;


import fy.mdm.bean.MaterialCodeBean;
import fy.mdm.bean.MaterialCodeChangeBean;


public class MaterialCodeDao {
	private boolean differentiateForm = false;
	private String[] materialtype;// = { "5", "5","5" };
	// ---------------
	private String[] materialCode;
	// private String[] newmaterialCode;
	private String[] newModel;
	private String[] newBrand;

	// 查下拉值 注意REF_ENUMID正式和测试不同
//	private final static String LOOKUP_XIALA_XINGJI = "SELECT c.SHOWVALUE FROM  CTP_ENUM_ITEM  c WHERE c.ENUMVALUE=? AND c.REF_ENUMID=?";
	private static Log log = LogFactory.getLog(MaterialCodeDao.class);

	// 编码查询原数据
	public JSONObject getMaterialInfo(String[] materialCode) throws Exception {

		JSONObject jo = new JSONObject();
		for (int j = 0; j < materialCode.length; j++) {
			MeasurementUnitMapping mum = new MeasurementUnitMapping();
			IMsgObject respMo = null;

			IServiceRequester Requester = BaseServiceRequester.getInstance();
			IMsgObject reqMo = MaterialCodeMqReqXml.add("06004",
					"06001000000013", materialCode[j]);

			log.info("reqMo：" + reqMo);
			respMo = Requester.execute(reqMo);

			log.info("respMo：" + respMo);
			MaterialCodeBean materialCodeBean = new MaterialCodeBean();
			materialCodeBean.setMaterialCode(materialCode[j]);
			List<GroupRecord> record2 = respMo.getResGroupRecord("ROW");

			for (GroupRecord groupRecord : record2) {
				if (differentiateForm) {

					materialCodeBean.setMaterialName(groupRecord
							.getFieldValue("MATERIEL_NAME"));
					materialCodeBean.setFirst_category(groupRecord
							.getFieldValue("FIRST_CATEGORY"));
				} else {
					// for (String materialtype2 : materialtype) {

					if ("5".equals(materialtype[j])) {

						materialCodeBean.setMateriel_describe(groupRecord
								.getFieldValue("MATERIEL_DESCRIBE"));

						materialCodeBean.setMeasurement(mum.getMeasurementUnit(
								"1", groupRecord.getFieldValue("MEASUREMENT")));

						materialCodeBean.setMateriel_describe_en(groupRecord
								.getFieldValue("MATERIEL_DESCRIBE_EN"));

						materialCodeBean.setMaterialtype(groupRecord
								.getFieldValue("BRAND_AND_MODELNUM"));

					} else {

						materialCodeBean.setMateriel_describe(groupRecord
								.getFieldValue("MATERIEL_DESCRIBE"));
						// materialCodeBean.setMeasurement( groupRecord
						// .getFieldValue("MEASUREMENT"));
						materialCodeBean.setMeasurement(mum.getMeasurementUnit(
								"1", groupRecord.getFieldValue("MEASUREMENT")));
						materialCodeBean.setMateriel_describe_en(groupRecord
								.getFieldValue("MATERIEL_DESCRIBE_EN"));
					}
				}

			}

			// }

			jo.put(materialCodeBean.getID(), materialCodeBean.getJSONObject());
		}

		log.info("jo：" + jo);
		return jo;
	}

	public void setdifferentiateForm(boolean differentiateForm) {
		this.differentiateForm = differentiateForm;
	}

	public void setMaterialtype(String[] materialtype) {
		this.materialtype = materialtype;
	}

	public JSONObject materialCodeChange() throws Exception {

		MaterialCodeChangeBean materialCodeChangeBean = new MaterialCodeChangeBean();

		JSONObject jo = new JSONObject();
		try {

			for (int i = 0; i < this.materialCode.length; i++) {// materialCodeChangeBean.getOldcodeList().size()

				IServiceRequester Requester = BaseServiceRequester
						.getInstance();
				List<GroupRecord> grs1 = new ArrayList<GroupRecord>();
				// 定义请求报文对象
				IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
				String SourceSysID = "06004"; // 请求方(发布方)
				String ServiceID = "06001000000033";// 响应方(发布方)
				// 获取系统时间
				Date date = new Date();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyyMMddHHmmss");
				String format = simpleDateFormat.format(date);
				// 对请求报文对象进行设置
				reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
				reqMo.setSerialNO(format.substring(0, 8)
						+ SourceSysID
						+ String.format("%06d",
								++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
				reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
				reqMo.setServiceDateTime(format); // 设置报文 日期时间
				GroupRecord gr1 = new GroupRecord();
				gr1.setName("ROW");
				gr1.setFieldValue("OLD_MATERIEL_CODE", materialCode[i]);
				gr1.setFieldValue("NEW_MATERIEL_CODE", "");
				gr1.setFieldValue("NEW_MATERIEL_DESCRIBE", "");
				gr1.setFieldValue("NEW_MEASUREMENT", "");
				gr1.setFieldValue("NEW_MATERIEL_DESCRIBE_EN", "");
				gr1.setFieldValue("CHANGE_TYPE", "");
				gr1.setFieldValue("CHANGE_REASION", "");
				gr1.setFieldValue("REMARK", "");
				gr1.setFieldValue("EXTEND_C", newModel[i]);
				gr1.setFieldValue("FONE_DISCRIBE_W", newBrand[i]);
				gr1.setFieldValue("MAIN_ATTR_DESC_OLD", "");
				gr1.setFieldValue("MAIN_ATTR_DESC_NEW", "");
				System.out.println(newModel[i]+"{");
				System.out.println(newBrand[i]+"}");
				grs1.add(gr1);
				reqMo.setReqGroupRecord(grs1);
				log.info("reqMo：" + reqMo);
				IMsgObject respMo = null;
				respMo = Requester.execute(reqMo);
				log.info("respMo：" + respMo);
				materialCodeChangeBean.setOldMaterielCode(materialCode[i]);
				List<GroupRecord> record2 = respMo.getResGroupRecord("ROW");
				for (GroupRecord groupRecord : record2) {

					materialCodeChangeBean.setStatus(groupRecord
							.getFieldValue("MDM_STATUS"));

					materialCodeChangeBean.setMessage(groupRecord
							.getFieldValue("MESSAGE"));
				}
				jo.put(materialCodeChangeBean.getID(),
						materialCodeChangeBean.getJSONObject());

			}
			log.info("jo：" + jo);
		} catch (Exception e) {
			e.printStackTrace();

		}
		return jo;
	}



	// public void setNewmaterialCode(String[] newmaterialCode) {
	// this.newmaterialCode = newmaterialCode;
	// }

	public void setNewModel(String[] newModel) {
		this.newModel = newModel;
	}

	public void setNewBrand(String[] newBrand) {
		this.newBrand = newBrand;
	}

	public String[] getMaterialCode() {
		return materialCode;
	}

	public void setMaterialCode(String[] materialCode) {
		this.materialCode = materialCode;
	}

	// public MaterialCodeChangeBean getMaterialCodeChangeBean() {
	// return materialCodeChangeBean;
	// }
	//
	// public void setMaterialCodeChangeBean(MaterialCodeChangeBean
	// materialCodeChangeBean) {
	// this.materialCodeChangeBean = materialCodeChangeBean;
	// }

}
